package com.wn.framework.decoration

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import dagger.hilt.android.scopes.ActivityScoped
import javax.inject.Inject

/**
 * @Author: wn
 * @Date: 2024/11/5 17:03
 * @Description: 瀑布流分割线
 */
@ActivityScoped
class StaggeredItemDecoration @Inject constructor() : RecyclerView.ItemDecoration() {
  // 分割线
  private var space = 10

  override fun getItemOffsets(
    outRect: Rect,
    view: View,
    parent: RecyclerView,
    state: RecyclerView.State,
  ) {
    super.getItemOffsets(outRect, view, parent, state)
    outRect.top = space
    // 瀑布流专属分割线
    val params = view.layoutParams as StaggeredGridLayoutManager.LayoutParams

    // 根据params.getSpanIndex()来判断左右边确定分割线,第一列设置左边距为space，右边距为space/2（第二列反之）
    if (params.spanIndex % 2 == 0) {
      outRect.left = space
      outRect.right = space / 2
    } else {
      outRect.left = space / 2
      outRect.right = space
    }
  }
}